package BT_46;

import java.util.LinkedList;
import java.util.List;

public class Solution {
	public List<List<Integer>> permute(int[] nums) {
		List<List<Integer>> res = new LinkedList<List<Integer>>();
		List<Integer> path = new LinkedList<Integer>();
		boolean[] used = new boolean[nums.length];
		if (nums.length == 0) {
			return res;
		}
		BT(nums, nums.length, 0, path, used, res);
		return res;

	}

	private void BT(int[] nums, int len, int place, List<Integer> path, boolean[] used, List<List<Integer>> res) {
		// TODO Auto-generated method stub
		if (place == len) {
			res.add(new LinkedList<Integer>(path));
			return;
		}
		for (int i = 0; i < len; i++) {
			if (!used[i]) {
				path.add(nums[i]);
				used[i]=true;
				BT(nums, len, place+1, path, used, res);
				used[i]=false;
				path.remove(path.size()-1);
				
			}
		}
	}

}
